package com.huawei.skinner.attrentry;

import android.content.Context;
import android.content.pm.PackageManager.NameNotFoundException;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;
import java.io.IOException;
import java.io.Serializable;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import o.a;
import o.d;
import o.SkinAttr;
import o.fkv;
import o.flh;
import o.flj;
import o.fll;
import o.fmq;
import o.foy;
import o.foz;
import o.fpd;
import o.fpg;

public class SkinAttrFactory {
    private static final fpd SKIN_ATTR_GROUPS = new fpd();
    private static final Map<AccessorKey, Class<? extends SkinAttr>> SUPPORT_SKIN_ATTRS = new fpg();
    private static boolean initialize = false;
    private static boolean loadedByPlugin;

    public static class AccessorKey implements Serializable {
        private static final long serialVersionUID = 2;
        private final String mAttrName;
        private final Class mViewWidget;

        public AccessorKey(String str, Class<? extends View> cls) {
            this.mAttrName = str;
            this.mViewWidget = cls;
        }

        public static AccessorKey build(String str, Class<? extends View> cls) {
            return new AccessorKey(str, cls);
        }

        public int hashCode() {
            return this.mAttrName.hashCode();
        }

        public boolean equals(Object obj) {
            if (!(obj instanceof AccessorKey)) {
                return false;
            }
            AccessorKey accessorKey = (AccessorKey) obj;
            if (accessorKey.mViewWidget.isAssignableFrom(this.mViewWidget) || this.mViewWidget.isAssignableFrom(accessorKey.mViewWidget)) {
                return true;
            }
            return false;
        }

        public String toString() {
            return "AFAK( attrName: " + this.mAttrName + " viewWidget: " + this.mViewWidget;
        }
    }

    static {
        SUPPORT_SKIN_ATTRS.put(new AccessorKey("textAppearance", TextView.class), a.class);
        SUPPORT_SKIN_ATTRS.put(new AccessorKey("style", View.class), d.class);
    }

    public static void init(Context context) {
        long j;
        NameNotFoundException e;
        long currentTimeMillis;
        StringBuilder append;
        IOException e2;
        InterruptedException e3;
        Object obj = null;
        long currentTimeMillis2 = System.currentTimeMillis();
        if (!initialize) {
            initialize = true;
            loadGeneratedSkinAttr();
            if (loadedByPlugin) {
                fmq.ˊ("Loaded roots graph by skinner gradle plugin!");
                j = currentTimeMillis2;
            } else {
                String str;
                Set ॱ;
                if (fkv.ˏ() || foy.ˊ(context)) {
                    fmq.ˊ("Run with debug mode or new install, rebuild SKIN_ATTR_GROUPS.");
                    try {
                        ॱ = foz.ॱ(context, "com.huawei.skinner.peanut");
                        try {
                            if (!ॱ.isEmpty()) {
                                context.getSharedPreferences("hw_skinner", 0).edit().putStringSet("SKINNER_ROOT_MAP", ॱ).apply();
                            }
                            foy.ˏ(context);
                        } catch (NameNotFoundException e4) {
                            e = e4;
                            fmq.ˎ("SkinAttrFactory.init() ERROR1 " + e.getMessage());
                            currentTimeMillis = System.currentTimeMillis();
                            append = new StringBuilder().append("Find module roots finished, roots size = ");
                            if (r0 != null) {
                                obj = Integer.valueOf(r0.size());
                            }
                            fmq.ˊ(append.append(obj).append(", cost ").append(currentTimeMillis - currentTimeMillis2).append(" ms.").toString());
                            for (String str2 : r0) {
                                str = "com.huawei.skinner.peanut.InterceptorGroup$$";
                                if (!str2.startsWith("com.huawei.skinner.peanut.SkinAttrRoot$$")) {
                                }
                                register(str2);
                            }
                            j = currentTimeMillis;
                            fmq.ˊ("Load root element finished, cost " + (System.currentTimeMillis() - j) + " ms.");
                            if (SKIN_ATTR_GROUPS.size() == 0) {
                                fmq.ˎ("No ISkinAttrRoot impl files were found, check your configuration please!");
                            }
                            if (!fkv.ˏ()) {
                                fmq.ˊ(String.format(Locale.getDefault(), "SKIN_ATTR_GROUP has already been loaded, SKIN_ATTR_GROUP[%d]", new Object[]{Integer.valueOf(SKIN_ATTR_GROUPS.size())}));
                            }
                        } catch (IOException e5) {
                            e2 = e5;
                            fmq.ˎ("SkinAttrFactory.init() ERROR2 " + e2.getMessage());
                            currentTimeMillis = System.currentTimeMillis();
                            append = new StringBuilder().append("Find module roots finished, roots size = ");
                            if (r0 != null) {
                                obj = Integer.valueOf(r0.size());
                            }
                            fmq.ˊ(append.append(obj).append(", cost ").append(currentTimeMillis - currentTimeMillis2).append(" ms.").toString());
                            for (String str22 : r0) {
                                str = "com.huawei.skinner.peanut.InterceptorGroup$$";
                                if (str22.startsWith("com.huawei.skinner.peanut.SkinAttrRoot$$")) {
                                }
                                register(str22);
                            }
                            j = currentTimeMillis;
                            fmq.ˊ("Load root element finished, cost " + (System.currentTimeMillis() - j) + " ms.");
                            if (SKIN_ATTR_GROUPS.size() == 0) {
                                fmq.ˎ("No ISkinAttrRoot impl files were found, check your configuration please!");
                            }
                            if (!fkv.ˏ()) {
                                fmq.ˊ(String.format(Locale.getDefault(), "SKIN_ATTR_GROUP has already been loaded, SKIN_ATTR_GROUP[%d]", new Object[]{Integer.valueOf(SKIN_ATTR_GROUPS.size())}));
                            }
                        } catch (InterruptedException e6) {
                            e3 = e6;
                            fmq.ˎ("SkinAttrFactory.init() ERROR3 " + e3.getMessage());
                            currentTimeMillis = System.currentTimeMillis();
                            append = new StringBuilder().append("Find module roots finished, roots size = ");
                            if (r0 != null) {
                                obj = Integer.valueOf(r0.size());
                            }
                            fmq.ˊ(append.append(obj).append(", cost ").append(currentTimeMillis - currentTimeMillis2).append(" ms.").toString());
                            for (String str222 : r0) {
                                str = "com.huawei.skinner.peanut.InterceptorGroup$$";
                                if (str222.startsWith("com.huawei.skinner.peanut.SkinAttrRoot$$")) {
                                }
                                register(str222);
                            }
                            j = currentTimeMillis;
                            fmq.ˊ("Load root element finished, cost " + (System.currentTimeMillis() - j) + " ms.");
                            if (SKIN_ATTR_GROUPS.size() == 0) {
                                fmq.ˎ("No ISkinAttrRoot impl files were found, check your configuration please!");
                            }
                            if (!fkv.ˏ()) {
                                fmq.ˊ(String.format(Locale.getDefault(), "SKIN_ATTR_GROUP has already been loaded, SKIN_ATTR_GROUP[%d]", new Object[]{Integer.valueOf(SKIN_ATTR_GROUPS.size())}));
                            }
                        }
                    } catch (NameNotFoundException e7) {
                        e = e7;
                        ॱ = null;
                        fmq.ˎ("SkinAttrFactory.init() ERROR1 " + e.getMessage());
                        currentTimeMillis = System.currentTimeMillis();
                        append = new StringBuilder().append("Find module roots finished, roots size = ");
                        if (r0 != null) {
                            obj = Integer.valueOf(r0.size());
                        }
                        fmq.ˊ(append.append(obj).append(", cost ").append(currentTimeMillis - currentTimeMillis2).append(" ms.").toString());
                        for (String str2222 : r0) {
                            str = "com.huawei.skinner.peanut.InterceptorGroup$$";
                            if (str2222.startsWith("com.huawei.skinner.peanut.SkinAttrRoot$$")) {
                            }
                            register(str2222);
                        }
                        j = currentTimeMillis;
                        fmq.ˊ("Load root element finished, cost " + (System.currentTimeMillis() - j) + " ms.");
                        if (SKIN_ATTR_GROUPS.size() == 0) {
                            fmq.ˎ("No ISkinAttrRoot impl files were found, check your configuration please!");
                        }
                        if (!fkv.ˏ()) {
                            fmq.ˊ(String.format(Locale.getDefault(), "SKIN_ATTR_GROUP has already been loaded, SKIN_ATTR_GROUP[%d]", new Object[]{Integer.valueOf(SKIN_ATTR_GROUPS.size())}));
                        }
                    } catch (IOException e8) {
                        e2 = e8;
                        ॱ = null;
                        fmq.ˎ("SkinAttrFactory.init() ERROR2 " + e2.getMessage());
                        currentTimeMillis = System.currentTimeMillis();
                        append = new StringBuilder().append("Find module roots finished, roots size = ");
                        if (r0 != null) {
                            obj = Integer.valueOf(r0.size());
                        }
                        fmq.ˊ(append.append(obj).append(", cost ").append(currentTimeMillis - currentTimeMillis2).append(" ms.").toString());
                        for (String str22222 : r0) {
                            str = "com.huawei.skinner.peanut.InterceptorGroup$$";
                            if (str22222.startsWith("com.huawei.skinner.peanut.SkinAttrRoot$$")) {
                            }
                            register(str22222);
                        }
                        j = currentTimeMillis;
                        fmq.ˊ("Load root element finished, cost " + (System.currentTimeMillis() - j) + " ms.");
                        if (SKIN_ATTR_GROUPS.size() == 0) {
                            fmq.ˎ("No ISkinAttrRoot impl files were found, check your configuration please!");
                        }
                        if (!fkv.ˏ()) {
                            fmq.ˊ(String.format(Locale.getDefault(), "SKIN_ATTR_GROUP has already been loaded, SKIN_ATTR_GROUP[%d]", new Object[]{Integer.valueOf(SKIN_ATTR_GROUPS.size())}));
                        }
                    } catch (InterruptedException e9) {
                        e3 = e9;
                        ॱ = null;
                        fmq.ˎ("SkinAttrFactory.init() ERROR3 " + e3.getMessage());
                        currentTimeMillis = System.currentTimeMillis();
                        append = new StringBuilder().append("Find module roots finished, roots size = ");
                        if (r0 != null) {
                            obj = Integer.valueOf(r0.size());
                        }
                        fmq.ˊ(append.append(obj).append(", cost ").append(currentTimeMillis - currentTimeMillis2).append(" ms.").toString());
                        for (String str222222 : r0) {
                            str = "com.huawei.skinner.peanut.InterceptorGroup$$";
                            if (str222222.startsWith("com.huawei.skinner.peanut.SkinAttrRoot$$")) {
                            }
                            register(str222222);
                        }
                        j = currentTimeMillis;
                        fmq.ˊ("Load root element finished, cost " + (System.currentTimeMillis() - j) + " ms.");
                        if (SKIN_ATTR_GROUPS.size() == 0) {
                            fmq.ˎ("No ISkinAttrRoot impl files were found, check your configuration please!");
                        }
                        if (!fkv.ˏ()) {
                            fmq.ˊ(String.format(Locale.getDefault(), "SKIN_ATTR_GROUP has already been loaded, SKIN_ATTR_GROUP[%d]", new Object[]{Integer.valueOf(SKIN_ATTR_GROUPS.size())}));
                        }
                    }
                }
                fmq.ˊ("Init moduleRoots from cache.");
                ॱ = new HashSet(context.getSharedPreferences("hw_skinner", 0).getStringSet("SKINNER_ROOT_MAP", new HashSet()));
                currentTimeMillis = System.currentTimeMillis();
                append = new StringBuilder().append("Find module roots finished, roots size = ");
                if (r0 != null) {
                    obj = Integer.valueOf(r0.size());
                }
                fmq.ˊ(append.append(obj).append(", cost ").append(currentTimeMillis - currentTimeMillis2).append(" ms.").toString());
                for (String str2222222 : r0) {
                    str = "com.huawei.skinner.peanut.InterceptorGroup$$";
                    if (str2222222.startsWith("com.huawei.skinner.peanut.SkinAttrRoot$$") || str2222222.startsWith(str)) {
                        register(str2222222);
                    }
                }
                j = currentTimeMillis;
            }
            fmq.ˊ("Load root element finished, cost " + (System.currentTimeMillis() - j) + " ms.");
            if (SKIN_ATTR_GROUPS.size() == 0) {
                fmq.ˎ("No ISkinAttrRoot impl files were found, check your configuration please!");
            }
            if (!fkv.ˏ()) {
                fmq.ˊ(String.format(Locale.getDefault(), "SKIN_ATTR_GROUP has already been loaded, SKIN_ATTR_GROUP[%d]", new Object[]{Integer.valueOf(SKIN_ATTR_GROUPS.size())}));
            }
        }
    }

    public static SkinAttr get(String str, int i, String str2, String str3, Class cls) {
        SkinAttr o_fko;
        InstantiationException e;
        IllegalAccessException e2;
        Class cls2 = (Class) SUPPORT_SKIN_ATTRS.get(new AccessorKey(str, cls));
        if (cls2 == null) {
            return null;
        }
        try {
            o_fko = (SkinAttr) cls2.newInstance();
            try {
                o_fko.attrValueRefName = str;
                o_fko.attrValueRefId = i;
                o_fko.ˋ = str2;
                o_fko.attrValueTypeName = str3;
                return o_fko;
            } catch (InstantiationException e3) {
                e = e3;
            } catch (IllegalAccessException e4) {
                e2 = e4;
                fmq.ˎ("SkinAttrFactory.get() ERROR2 " + e2.getMessage());
                return o_fko;
            }
        } catch (InstantiationException e5) {
            InstantiationException instantiationException = e5;
            o_fko = null;
            e = instantiationException;
            fmq.ˎ("SkinAttrFactory.get() ERROR1 " + e.getMessage());
            return o_fko;
        } catch (IllegalAccessException e6) {
            IllegalAccessException illegalAccessException = e6;
            o_fko = null;
            e2 = illegalAccessException;
            fmq.ˎ("SkinAttrFactory.get() ERROR2 " + e2.getMessage());
            return o_fko;
        }
    }

    public static boolean isSupportedAttr(String str, Class<? extends View> cls) {
        if (SUPPORT_SKIN_ATTRS.get(new AccessorKey(str, cls)) != null) {
            return true;
        }
        List<String> arrayList = new ArrayList();
        registerSkinAttrGroup(cls, arrayList);
        if (arrayList.size() > 0) {
            for (String remove : arrayList) {
                SKIN_ATTR_GROUPS.remove(remove);
            }
        }
        if (SUPPORT_SKIN_ATTRS.get(new AccessorKey(str, cls)) != null) {
            return true;
        }
        fmq.ˊ("SkinAttrFactory.isSupportedAttr(): Not support :attrname " + str + " View: " + cls.getName());
        return false;
    }

    public static void registerSkinAttrRoot(flj o_flj) {
        if (o_flj != null) {
            loadedByPlugin = true;
            o_flj.ॱ(SKIN_ATTR_GROUPS);
        }
    }

    public static void registerSkinAttr(String str, Class cls, Class cls2) {
        registerSkinAttrInternal(new AccessorKey(str, cls), cls2);
    }

    private static void registerSkinAttrInternal(AccessorKey accessorKey, Class cls) {
        if (SkinAttr.class.equals(cls.getSuperclass())) {
            fmq.ˊ("register class Success classname:" + cls.getCanonicalName());
            SUPPORT_SKIN_ATTRS.put(accessorKey, cls);
        }
    }

    private static void registerSkinAttrGroup(Class<? extends View> cls, List<String> list) {
        String canonicalName = cls.getCanonicalName();
        String[] ˎ = SKIN_ATTR_GROUPS.ˎ(canonicalName);
        if (ˎ != null && ˎ.length > 0) {
            for (String cls2 : ˎ) {
                try {
                    Object newInstance = Class.forName(cls2).getConstructor(new Class[0]).newInstance(new Object[0]);
                    if (newInstance instanceof fll) {
                        ((fll) newInstance).ˏ(SUPPORT_SKIN_ATTRS);
                    }
                } catch (InstantiationException e) {
                    fmq.ˎ("SkinAttrFactory.registerSkinAttrGroup() ERROR1 " + e.getMessage());
                } catch (IllegalAccessException e2) {
                    fmq.ˎ("SkinAttrFactory.registerSkinAttrGroup() ERROR2 " + e2.getMessage());
                } catch (InvocationTargetException e3) {
                    fmq.ˎ("SkinAttrFactory.registerSkinAttrGroup() ERROR3 " + e3.getMessage());
                } catch (NoSuchMethodException e4) {
                    fmq.ˎ("SkinAttrFactory.registerSkinAttrGroup() ERROR4 " + e4.getMessage());
                } catch (ClassNotFoundException e5) {
                    fmq.ˎ("SkinAttrFactory.registerSkinAttrGroup() ERROR5 " + e5.getMessage());
                }
            }
            list.add(canonicalName);
        }
        Class superclass = cls.getSuperclass();
        if (!superclass.equals(Object.class)) {
            registerSkinAttrGroup(superclass, list);
        }
    }

    private static void loadGeneratedSkinAttr() {
        loadedByPlugin = false;
        register("com.huawei.skinner.peanut.SkinAttrRoot$$skinner");
        register("com.huawei.skinner.peanut.SkinAttrRoot$$skinner_hwwidget_adapter");
    }

    private static void register(String str) {
        fmq.ˊ("register class start, classname:" + str);
        if (!TextUtils.isEmpty(str)) {
            try {
                Object newInstance = Class.forName(str).getConstructor(new Class[0]).newInstance(new Object[0]);
                if (newInstance instanceof flj) {
                    registerSkinAttrRoot((flj) newInstance);
                } else if (newInstance instanceof flh) {
                    ((flh) newInstance).ˋ();
                } else {
                    fmq.ˊ("register failed, class name: " + str + " should implements ISkinAttrRoot");
                }
            } catch (Exception e) {
                fmq.ˎ("register class error:" + e);
            }
        }
    }
}
